﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using Azad;
using Azad.BCL;
using NazSoft.Framework;

namespace Newsweek
{
    public partial class Site1 : System.Web.UI.MasterPage
    {
        DateTime? _today = null;
        BCL.Entity.LogoInformation _logoInformation = null;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.AppRelativeCurrentExecutionFilePath.ToLowerInvariant().Contains("error.aspx"))
            {
                return;
            }

            SilverlightService silverlightService = new SilverlightService();
            var menuName = Request["mn"] ?? string.Empty;


            if (!IsPostBack)
            {
                #region bind archives
                var archives = silverlightService.ArchiveDate(null);


                Dictionary<String, String> dates = new Dictionary<string, string>();

                //CurrentMagDateContainer.Text = _logoInformation.FromDate.ToString("MMM dd") + " - " + _logoInformation.CurrentDate.ToString("MMM dd") + ", " + _logoInformation.CurrentDate.Year;

                foreach (var item in archives)
                {
                    var magazine_name = item.FromDate.ToString("MMM dd") + " - " + item.CurrentDate.ToString("MMM dd") + ", " + item.CurrentDate.Year;

                    if (String.IsNullOrEmpty(item.Url) == false)
                    {
                        dates.Add(item.Url, magazine_name);
                    }
                }

                //dropdownDate.DataSource = dates;
                //dropdownDate.DataBind();
                //dropdownDate.Items.Insert(0, "Please select date");


                var magazines = archives.ToList();

                if (magazines != null)
                {
                    while (magazines.Count < 4 && magazines.Count > 0)
                    {
                        magazines.Add(magazines[0]);
                    }
                }

                MagRepeater1.DataSource = magazines.OrderByDescending(f => f.Id);
                MagRepeater1.DataBind();
                #endregion


                    
                bool preview = Convert.ToBoolean(Request.QueryString["p"]);
                var todayUrl = DateTimeUtilities.GetLogoDateFromQueryString(Request.QueryString);
                String logoInfoUrl = DateTimeUtilities.GetLogoDateFromQueryString(Request.QueryString);
                _logoInformation = silverlightService.GetByCurrentId(todayUrl, preview);
                if (_logoInformation != null)
                {
                    //imgFarman.ImageUrl = logoInfo.FarmanImage;
                    //Logo.ImageUrl = logoInfo.LogoImage;
                    //Heading.ImageUrl = logoInfo.HeaderImage;


                    // over-ride date here to be used later in databound method
                    _today = _logoInformation.CurrentDate;




                    CurrentMagDateContainer.Text = _logoInformation.FromDate.ToString("MMM dd") + " - " + _logoInformation.CurrentDate.ToString("MMM dd") + ", " + _logoInformation.CurrentDate.Year;
                    imgCurrentMagzine.Src = _logoInformation.FarmanImage;

                    this.Page.Title = "News Week | " + CurrentMagDateContainer.Text + " | " + menuName.Replace("-", " ").Capitalize();
                    //this.logoImage.Src = _logoInformation.Theme.LogoImage;
                    string style = @"<style type=""text/css"">
                        .nav-bar {background-image: url('" + _logoInformation.Theme.MenuBackgroundImageUrl + @"');background-repeat: repeat-x;} 
                        .nav-bar a {color: #" + _logoInformation.Theme.FontColor + @";}
                        .NavigationRight a.button {color: #" + _logoInformation.Theme.FontColor + @";}
                        .Footer {background-image: url('" + _logoInformation.Theme.FooterImage + @"');background-repeat: no-repeat;}
                    </style>";

                    this.Page.Header.Controls.Add(new Literal() { Text = style });


                    //HomeIconImage.ImageUrl = _logoInformation.Theme.HomeIconUrl;
                    ////ContactUsIconImage.ImageUrl = _logoInformation.Theme.ContactUsIconUrl;
                    //CurrentMagazineIconImage.ImageUrl = _logoInformation.Theme.CurrentMagazineIconUrl;

                    //dateInfo.Add("y", today.Value.Year.ToString());
                    //dateInfo.Add("m", today.Value.Month.ToString());
                    //dateInfo.Add("d", today.Value.Day.ToString());
                }
                else
                {
                    //imgFarman.Visible = false;
                    //Logo.Visible = false;
                    //Heading.Visible = false;


                    return;
                }



                // get menu and then bind
                // do not move this code anywhere
                // becuase in the above code, the date is set
                // which is used in the databound method of this menu
                var menu = silverlightService.GetMenuByUrl(logoInfoUrl);
                lstMenu.DataSource = menu.Where(f => f.LookUpID != 11).ToList(); //.Where(x => x.LookUpID != 12).Where(c => c.LookUpID != 13);
                lstMenu.DataBind();
            }

            CurrentMagzine.HRef = _logoInformation.Url + "current-magazine";
            lnkIdaria.HRef = _logoInformation.Url + "Idaria";
            if (menuName == "current-magazine")
            {
                //content_body_container_div.Attributes["class"] = "middle current-magazine";
                //content_body_left_container.Visible = false;
            }
            else
            {
                #region bind commercials
                if (_today.HasValue)
                {
                    var commercials = silverlightService.GetCommercial(_today.Value);
                    if (commercials != null)
                    {
                        #region Comments
                        //foreach (var commercial in commercials)
                        //{
                        //    switch (commercial.TypeId)
                        //    {
                        //        case 101:
                        //            {
                        //                CommercialVerticalLarge.InnerHtml += commercial.Script;
                        //            }
                        //            break;
                        //        case 102:
                        //            {
                        //                CommercialLeftVertical1.InnerHtml += commercial.Script;
                        //            }
                        //            break;
                        //        case 103:
                        //            {
                        //                CommercialLeftVertical2.InnerHtml += commercial.Script;
                        //            }
                        //            break;
                        //        case 104:
                        //            {
                        //                CommercialRightVertical1.InnerHtml += commercial.Script;
                        //            }
                        //            break;
                        //        case 105:
                        //            {
                        //                CommercialRightVertical2.InnerHtml += commercial.Script;
                        //            }
                        //            break;
                        //        case 106:
                        //            {
                        //                CommercialBottomHorizontal.InnerHtml = commercial.Script;
                        //            }
                        //            break;
                        //        default:
                        //            break;
                        //    }
                        //}
                        #endregion
                        //rptLeftCommercial.DataSource = commercials.Where(f => f.TypeId == 102);
                        //rptLeftCommercial.DataBind();
                        rptRightSection.DataSource = commercials.Where(f => f.TypeId == 101);
                        rptRightSection.DataBind();
                        //rptBottom.DataSource = commercials.Where(f => f.TypeId == 106);
                        //rptBottom.DataBind();
                    }
                }
                #endregion
            }




        }
        protected void lstMenu_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                var lookup = (BCL.Entity.LookUp)e.Item.DataItem;
                if (lookup != null)
                {
                    var newsTypeHyperlink = (HtmlAnchor)e.Item.FindControl("NewsTypeHyperlink");
                    if (newsTypeHyperlink != null)
                    {
                        string menuName = lookup.Description;

                        var newsCount = _logoInformation.Detail.Where(x => x.MenuId == lookup.LookUpID).Count();

                        if (newsCount > 0)
                        {
                            newsTypeHyperlink.Visible = true;
                            //newsTypeHyperlink.Attributes["style"] = "background: url('" + _logoInformation.Theme.ButtonImageUrl + "') no-repeat;";

                            //var today = DateTimeUtilities.GetDateFromQueryString();
                            if (_today.HasValue)
                            {
                                var navigateUrl = string.Format("{0}{1}", _logoInformation.Url, menuName);
                                newsTypeHyperlink.HRef = navigateUrl;
                            }

                            if (lookup.LookUpID == 11)
                            {
                                newsTypeHyperlink.Target = "_blank";
                                //newsTypeHyperlink.HRef = newsTypeHyperlink.HRef + "?mp=b";
                            }
                        }
                        else
                        {
                            newsTypeHyperlink.Visible = false;
                        }
                    }
                }
            }
        }
    }
}